<?php include '../base/header.php' ?>
<?php
require_once '../sql/connection.php';
$commodity_id = $_GET['com_id'];
// 查询商品
$sql_config = "select * from commodity WHERE ID = $commodity_id";
// 连接数据库，并查询
$result_config = mysqli_query($GLOBALS['conn'], $sql_config);
// 获取商品信息
$row = mysqli_fetch_array($result_config);
// 购买数量
$buy_number = $_POST['buy_number'];
// 总价
$sum = $_POST['buy_number'] * $row[3];
// 剩余库存
$stock = ($row[4] - $buy_number);
// 更新库存
$update_sql = "UPDATE commodity SET stock = $stock  WHERE name = '$row[1]'";
// 连接数据库，并查询
$result_config = mysqli_query($GLOBALS['conn'], $update_sql);

// 获取表单数据
$username = $_POST['username'];
$address = $_POST['address'];
$telephone_number = $_POST['telephone_number'];
$buy_number = $_POST['username'];
$postcode = $_POST['postcode'];

// 插入订单信息
$insert_sql = "INSERT INTO delivery_information (user_name ,address,telephone_number ,postcode,buy_number, com_name) 
VALUES ('$username','$address','$telephone_number','$postcode',$buy_number,'$row[1]')";
// mysqli_query($GLOBALS['conn'], $insert_sql);
// 连接数据库，并查询
if (mysqli_query($GLOBALS['conn'], $insert_sql)) {
    echo "<script>alert('数据写入成功！');</script>";
} else {
    echo "<script>alert('数据写入失败！');</script>";
}
?>


<div class="uk-card uk-card-default uk-card-body uk-width-auto uk-card-large uk-text-center">
    <h3 class="uk-card-title">下单成功！请您耐心等待！</h3>
    <form class="uk-form-horizontal uk-margin-large">

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">商品</label>
            <div class="uk-form-controls">
                <input class="uk-input" type="text" placeholder="<?php echo $row[1] ?>"
                       readonly="readonly">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">姓名</label>
            <div class="uk-form-controls">
                <input class="uk-input" type="text" placeholder="<?php echo $username ?>"
                       readonly="readonly">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">地址</label>
            <div class="uk-form-controls">
                <input class="uk-input" placeholder="<?php echo $address ?>"
                       readonly="readonly">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">电话号码</label>
            <div class="uk-form-controls">
                <input class="uk-input" type="text"
                       placeholder="<?php echo $telephone_number ?>"
                       readonly="readonly">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">邮编</label>
            <div class="uk-form-controls">
                <input class="uk-input" type="text" placeholder="<?php echo $postcode ?>"
                       readonly="readonly">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">购买数量</label>
            <div class="uk-form-controls">
                <input class="uk-input" placeholder="<?php echo $buy_number ?>"
                       readonly="readonly"
                       type="text">
            </div>
        </div>

        <div class="uk-margin">
            <label class="uk-form-label" for="form-horizontal-text">总价</label>
            <div class="uk-form-controls">
                <input class="uk-input" type="text" placeholder="<?php echo $sum ?>￥"
                       readonly="readonly">
            </div>
        </div>


    </form>
    <a href="index.php" class="uk-link-muted">
        <button class="uk-button uk-button-primary">
            返回首页
        </button>
    </a>
</div>


<?php include '../base/footer.php' ?>
